/**
 * 
 */
package systole.view.crud.sport;

import java.awt.Frame;
import java.util.Calendar;
import javax.swing.JDialog;

import systole.domain.clinicalInformation.Sport;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;

/**
 * @author jmj
 *
 */
public class ControllerSportEdition extends ControllerEntityEdition {

    /**
     */
    public ControllerSportEdition() {
        super();
        this.entityName = "Actividad Física";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Sport sportToLoad = (Sport) this.curretnEntity;
        JDialogSport formToLoad = (JDialogSport) this.editForm;

        sportToLoad.setName(formToLoad.getjEdtName().getText());
        sportToLoad.setDescription(formToLoad.getjTxtDescription().getText());
        sportToLoad.setLastUpdate(Calendar.getInstance());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogSport formToLoad = (JDialogSport) this.editForm;
        Sport currentSport = (Sport) this.curretnEntity;
        formToLoad.getjEdtName().setText(currentSport.getName());
        formToLoad.getjTxtDescription().setText(currentSport.getDescription());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogSport formToValid = (JDialogSport) this.editForm;
        Sport sport = (Sport) this.curretnEntity;

        if ((formToValid.getjEdtName().getText() == null) || (formToValid.getjEdtName().getText().isEmpty())) {
            return "<html>Debe ingresar el nombre de la actividad física</html>";
        }
        try {
            if (this.facadeDB.getSportBroker().existSport(formToValid.getjEdtName().getText(), sport.getId())) {
                return "<html>Ya existe una actividad física con el nombre ingresado</html>";
            }

        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }
        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
        if (this.isEditing()) {
            this.facadeDB.getSportBroker().update((Sport) this.curretnEntity);
        } else {
            this.facadeDB.getSportBroker().insert((Sport) this.curretnEntity);
        }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/sport16.png"));
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new Sport();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogSport(parent, this);
    }
}
